/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.ranger.service;

import org.apache.ranger.common.SearchField;
import org.apache.ranger.entity.XXAuditMap;
import org.apache.ranger.entity.XXPortalUser;
import org.apache.ranger.view.VXAuditMap;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

@Service
@Scope("singleton")
public class XAuditMapService extends XAuditMapServiceBase<XXAuditMap, VXAuditMap> {
    public XAuditMapService() {
        searchFields.add(new SearchField("resourceId", "obj.resourceId", SearchField.DATA_TYPE.INTEGER, SearchField.SEARCH_TYPE.FULL));
        searchFields.add(new SearchField("userId", "obj.userId", SearchField.DATA_TYPE.INTEGER, SearchField.SEARCH_TYPE.FULL));
        searchFields.add(new SearchField("groupId", "obj.groupId", SearchField.DATA_TYPE.INTEGER, SearchField.SEARCH_TYPE.FULL));
    }

    @Override
    protected void validateForCreate(VXAuditMap vObj) {
        // TODO Auto-generated method stub
    }

    @Override
    protected void validateForUpdate(VXAuditMap vObj, XXAuditMap mObj) {
        // TODO Auto-generated method stub
    }

    @Override
    protected XXAuditMap mapViewToEntityBean(VXAuditMap vObj, XXAuditMap mObj, int operationContext) {
        XXAuditMap ret = null;

        if (vObj != null && mObj != null) {
            ret = super.mapViewToEntityBean(vObj, mObj, operationContext);

            if (ret.getAddedByUserId() == null || ret.getAddedByUserId() == 0) {
                if (!stringUtil.isEmpty(vObj.getOwner())) {
                    XXPortalUser xXPortalUser = daoManager.getXXPortalUser().findByLoginId(vObj.getOwner());

                    if (xXPortalUser != null) {
                        ret.setAddedByUserId(xXPortalUser.getId());
                    }
                }
            }

            if (ret.getUpdatedByUserId() == null || ret.getUpdatedByUserId() == 0) {
                if (!stringUtil.isEmpty(vObj.getUpdatedBy())) {
                    XXPortalUser xXPortalUser = daoManager.getXXPortalUser().findByLoginId(vObj.getUpdatedBy());

                    if (xXPortalUser != null) {
                        ret.setUpdatedByUserId(xXPortalUser.getId());
                    }
                }
            }
        }

        return ret;
    }

    @Override
    protected VXAuditMap mapEntityToViewBean(VXAuditMap vObj, XXAuditMap mObj) {
        VXAuditMap ret = null;

        if (mObj != null && vObj != null) {
            ret = super.mapEntityToViewBean(vObj, mObj);

            if (stringUtil.isEmpty(ret.getOwner())) {
                XXPortalUser xXPortalUser = daoManager.getXXPortalUser().getById(mObj.getAddedByUserId());

                if (xXPortalUser != null) {
                    ret.setOwner(xXPortalUser.getLoginId());
                }
            }

            if (stringUtil.isEmpty(ret.getUpdatedBy())) {
                XXPortalUser xXPortalUser = daoManager.getXXPortalUser().getById(mObj.getUpdatedByUserId());

                if (xXPortalUser != null) {
                    ret.setUpdatedBy(xXPortalUser.getLoginId());
                }
            }
        }

        return ret;
    }
}
